package start;

import java.util.ArrayList;
import java.util.List;
import java.io.*;
import jaxb.folderConf.*;

import jaxb.server.Servconfig;

public class StrongboxAdd {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(args.length != 1){
			System.err.println("Usage: StrongboxAdd <user name>");
			System.exit(-1);
		}
		
		if(!(new File("serverconf.xml").exists())){
			System.err.println("Server uninitialized, run Strongbox server first!");
			System.exit(-1);
		}
		
		Servconfig conf = Servconfig.getFromFile("serverconf.xml");
		//creating user folder
		String userPath = conf.getFolders().getRoot()+"/"+conf.getFolders().getUser();
		(new File(userPath+"/"+args[0])).mkdir();
		//creating user conf
		String userConf = conf.getFolders().getRoot()+"/"+conf.getFolders().getConfig();
		(new File(userConf+"/"+args[0]+"/config")).mkdirs();
		(new File(userConf+"/"+args[0]+"/logs")).mkdir();
		
		Config uConf = new Config();
		List<Folder> l = new ArrayList<Folder>();
		Folder f = new Folder();
		f.setUserid(args[0]);
		f.setRole("OWNER");
		l.add(f);
		uConf.setUser(l);
		uConf.saveToFile(userConf+"/"+args[0]+"/config/config.xml");
		
		System.out.println("User added!(Be sure to add this user to Kerberos too)");
		
	}

}
